package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.LLApplication;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.ChooseBankDialog;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BankInfo;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqBankList;
import com.lanlian.commonlib.bean.ReqWithdraw;
import com.lanlian.commonlib.bean.RespBankList;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.imagelib.ShowImageUtils;
import com.lanlian.utillib.CommonUtils;
import com.lanlian.utillib.MoneyTextWatcher;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/25 14:59
 *     desc   : 提现申请页面
 *     version: 1.0
 * </pre>
 */
public class WithdrawActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvBankName;
    private TextView mTvBankCard;
    private EditText mEtMoney;
    private TextView mTvAll;
    private TextView mTvAvailableMoney;
    private TextView mTvSubmit;
    private LinearLayout mLayBank;
    private LinearLayout mLayNoBank;
    private ImageView mImgBankLogo;

    private ArrayList<BankInfo> mBankList;
    private BankInfo mBankInfo;
    private ChooseBankDialog mChooseBankDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw, true);
        initView();
        setListeners();
        getBankList();
        mTvAvailableMoney.setText(String.format("$ %.2f", LLApplication.getAvailableFunds()));
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("提现");
        mTvBankName = findViewById(R.id.tv_bank_name);
        mTvBankCard = findViewById(R.id.tv_bank_card);
        mEtMoney = findViewById(R.id.et_money);
        mTvAll = findViewById(R.id.tv_all);
        mTvAvailableMoney = findViewById(R.id.tv_available_money);
        mTvSubmit = findViewById(R.id.tv_submit);
        mLayBank = findViewById(R.id.lay_bank);
        mLayNoBank = findViewById(R.id.lay_no_bank);
        mImgBankLogo = findViewById(R.id.img_bank_logo);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvAll.setOnClickListener(this);
        mTvSubmit.setOnClickListener(this);
        mLayBank.setOnClickListener(this);
        //默认两位小数
        mEtMoney.addTextChangedListener(new MoneyTextWatcher(mEtMoney));
    }

    /**
     * 获取银行卡列表
     */
    private void getBankList() {
        ReqBankList reqBankList = new ReqBankList();
        reqBankList.setType(2);
        VolleyRequestUtils.getInstance().getBankList(this.toString(), reqBankList, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                RespBankList respBankList = (RespBankList) response;
                mBankList = respBankList.getData();
                if (!CommonUtils.isListEmpty(mBankList)) {
                    mLayBank.setVisibility(View.VISIBLE);
                    mLayNoBank.setVisibility(View.GONE);
                    mTvSubmit.setEnabled(true);
                    mTvSubmit.setBackgroundResource(R.mipmap.login_btn);
                    mBankInfo = mBankList.get(0);
                    setBankInfo();
                } else {
                    mLayBank.setVisibility(View.GONE);
                    mLayNoBank.setVisibility(View.VISIBLE);
                    mTvSubmit.setEnabled(false);
                    mTvSubmit.setBackgroundResource(R.drawable.bg_click_false);
                }
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                showToast(baseResponse.getMsg());
                finish();
            }
        });
    }

    private void setBankInfo() {
        String bankCard = mBankInfo.getCardNo();
        ShowImageUtils.showImageView(WithdrawActivity.this, mBankInfo.getLogo(),
                SystemConfig.IMG_DEFAULT_BACKGROUND, mImgBankLogo);
        mTvBankName.setText(mBankInfo.getBankName());
        mTvBankCard.setText(String.format("尾号%s储蓄卡", bankCard.substring(bankCard.length() - 4, bankCard.length())));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_all:
                mEtMoney.setText(String.format("%.2f", LLApplication.getAvailableFunds()));
                break;
            case R.id.tv_submit:
                if (isFastClick()) {
                    return;
                }
                if (TextUtils.isEmpty(mEtMoney.getText().toString())) {
                    showToast("提现金额不能为空！");
                    return;
                }
                ReqWithdraw reqWithdraw = new ReqWithdraw();
                reqWithdraw.setSourceCurrency("USD");
                reqWithdraw.setSourceMoney(mEtMoney.getText().toString());
                reqWithdraw.setUserBankId(mBankInfo.getId());
                mTvSubmit.setClickable(false);
                showLoadingDialog();
                VolleyRequestUtils.getInstance().withdraw(this.toString(), reqWithdraw, new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        dismissLoadingDialog();
                        showToast("提现申请已提交");
                        EventBusUtils.post(new EventMessage(EventCode.EVENT_FUNDS_REFRESH));
                        finish();
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        mTvSubmit.setClickable(true);
                        dismissLoadingDialog();
                        showToast(baseResponse.getMsg());
                    }
                });
                break;
            case R.id.lay_bank:
                if (CommonUtils.isListEmpty(mBankList)) {
                    return;
                }
                if (mChooseBankDialog == null) {
                    mChooseBankDialog = new ChooseBankDialog(this, mBankList, new ChooseBankDialog.OnChooseBankListener() {
                        @Override
                        public void onChooseBank(BankInfo bankInfo) {
                            mBankInfo = bankInfo;
                            setBankInfo();
                        }
                    });
                }
                mChooseBankDialog.showDialog();
                break;
            default:
                break;
        }
    }

}
